using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Newtonsoft.Json;

public class DataManager : MonoBehaviour
{
   public static DataManager instance{get; private set;}
   public float musicSetting;
   public float sfxSetting;

   void Awake()
   {
      if(instance != null)
      {
         Destroy(gameObject);
         return;
      }
      instance = this;
      DontDestroyOnLoad(gameObject);
   }
//    [System.Serializable]
   class SaveData
   {
        public float musicSetting;
        public float sfxSetting;

   }

   public void SaveMusicSetting()
   {
      SaveData data = new SaveData();
      data.musicSetting = musicSetting;

      var json=JsonConvert.SerializeObject(musicSetting);
      File.WriteAllText(Application.persistentDataPath + "/saveMusicFile.json", json);
   }

   public void LoadMusicSetting()
   {
        string path=Application.persistentDataPath + "/saveMusicFile.json";
        if(File.Exists(path))
        {
            string json=File.ReadAllText(path);
            musicSetting=(float)JsonConvert.DeserializeObject(json,typeof(float));
        }
        else
        {
            musicSetting=1.0f;
        }

   }

   public void SaveSfxSetting()
   {
      SaveData data = new SaveData();
      data.sfxSetting = sfxSetting;

      var json=JsonConvert.SerializeObject(sfxSetting);
      File.WriteAllText(Application.persistentDataPath + "/saveSfxFile.json", json);
   }

   public void LoadSfxSetting()
   {
        string path=Application.persistentDataPath + "/saveSfxFile.json";
        if(File.Exists(path))
        {
            string json=File.ReadAllText(path);
            sfxSetting=(float)JsonConvert.DeserializeObject(json,typeof(float));
        }
        else
        {
            sfxSetting=1.0f;
        }

   }
}
